package lxm.demo.ultra.custom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.john.waveview.WaveView;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;
import lxm.demo.ultra.R;


public class WaveCustomPtrHeader extends FrameLayout implements PtrUIHandler {
    int i;
    WaveView waveView;

    public WaveCustomPtrHeader(Context context) {
        super(context);
        init();
    }

    public WaveCustomPtrHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WaveCustomPtrHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @SuppressLint("NewApi")
    public WaveCustomPtrHeader(Context context, AttributeSet attrs,
                               int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(
                R.layout.layout_header_wave, this);
        waveView = (WaveView) view.findViewById(R.id.wave_view);
    }


    public void setWaveProgress(int progress) {
        waveView.setProgress(progress);
        invalidate();
    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {

    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {

    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        postDelayed(new Runnable() {
            @Override
            public void run() {
//                try {
//                    for (int i = 0; i <= 100; i++) {
//                        Thread.sleep(100);
//                        Log.i("----", "progress----:" + i);
                        waveView.setProgress(40);
                        postInvalidate();
//                        if (i == 100) {
//                            return;
//                        }
//                    }
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
        }, 100);


    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
        float percent = Math.min(1f, ptrIndicator.getCurrentPercent());
        int per = (int) (percent * 100 * 1.0);
        waveView.setProgress(per);
        invalidate();
    }
}
